using JT808Protocol.Common;
using JT808Protocol.Models.Enums;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace JT808Protocol.Models.BodyParserStrategy.DeviceParams
{
    /// <summary>
    /// WORD 型参数
    /// </summary>
    public class DpWordUshort : IDeviceParam
    {
        [JsonIgnore]
        public uint ParamID { get; set; }

        [JsonIgnore]
        public byte Length { get; set; }

        [JsonPropertyName("参数类型")]
        public EnumDeviceParamType ParamType
        {
            get
            {
                try
                {
                    return (EnumDeviceParamType)ParamID;
                }
                catch (Exception)
                {
                    return EnumDeviceParamType.保留;
                }
            }
        }

        [JsonPropertyName("参数值")]
        public ushort Data { get; set; }

        public DpWordUshort(uint id)
        {
            ParamID = id;
        }

        public DpWordUshort(uint id, ushort data)
        {
            ParamID = id;
            Data = data;
        }

        public void ReadEntity(ref ReadOnlySpan<byte> source, ref int startIndex)
        {
            ParamID = ParseHelper.Uint32(ref source, startIndex);
            startIndex += 4;
            Length = source[startIndex];
            startIndex++;
            Data = ParseHelper.Uint16(ref source, startIndex);
            startIndex += Length;
        }

        public List<byte> WriteToNetBytes()
        {
            List<byte> result = new List<byte>();
            result.AddRange(ParseHelper.Bytes(ParamID));
            result.Add(0x01);
            result.AddRange(ParseHelper.Bytes(Data));
            return result;
        }
    }
}